// pages/information/information.js
const app = getApp()
var service = require('../../util/service');
var util = require('../../util/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: {
      currentCity: "",
      placestr: ''
    },
    tabbar: {},
    headline:[],
    informationList:[],
    lodding: false,
    loddtext: "加载中……",
    page:1,
    pageSize:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.changeTabBar(); 
    that.getTopics();
    service.AJAX("smc/topic/NewTopics/queryNewTopics.json", { page: 1,pageSize:2, $top:1 }, res => {
      var informationList = res.rows;
      if (informationList){
        var headline = [];
        for (var i = 0; i < informationList.length; i++) {
          if (i <= 1) {
            headline.push({ id: informationList[i].topicId, name: informationList[i].topicName });
          }

        }
        that.setData({
          headline: headline
        })
      }
    }, err => { }, 'POST')
  },
  getTopics(){
    var that = this;
    that.setData({
      lodding: true,
      loddtext: "加载中……"
    })
    service.AJAX("smc/topic/NewTopics/queryNewTopics.json", { page: that.data.page, pageSize: that.data.pageSize }, res => {
      var informationList = res.rows;
      var lodd = informationList.length < 10;
      console.log(informationList);
      for (var i = 0; i < informationList.length; i++) {
        informationList[i].imgs = informationList[i].pic && informationList[i].pic.split(",");
        for (var j = 0; j < informationList[i].imgs.length; j++) {
          informationList[i].imgs[j] = service.imgurl + informationList[i].imgs[j];
        }
        informationList[i].createdAt = util.getTime(informationList[i].createdAt);
      }
      var data = that.data.informationList.concat(informationList);
      that.setData({
        informationList: data,
        lodding: lodd,
        loddtext: lodd ? "没有更多数据了" : "加载中……"
      })
    }, err => { }, 'POST')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getpol(
        function (currentCity) {
          that.setData({
            'head.currentCity': currentCity
          })
        }
    )
    var user = app.userInfo;
    if (!user || !user.pic || user.pic == "") {
      wx.navigateBack();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  lower: function () {
    this.setData({
      'page': this.data.page + 1
    })
    this.getTopics();
  }
})